<?php
include('functions.inc.php');
$link = db_connect();

if (isset($argc) && $argc < 2) {
	die("Usage: php $argv[0] filename.opml\n");
}

// Create tables, if needed
include('create_tables.inc.php');

if (isset($argc)) {
	$file = $argv[1];
} else {
	$file = $_GET['file'];
}
if (!file_exists($file)) {
	die("Failed to open $file\n");
}

// PHP 5 only // $xml = simplexml_load_file($file);
// Replaced by simplexml44 below
$impl = new IsterXmlSimpleXMLImpl();
$xml = $impl->load_file($file);

foreach ($xml->opml->body->outline as $outline) {
	$attr = $outline->attributes();
	$title = $attr['title'];
	$url = $attr['xmlUrl'];
	var_dump($title);
	var_dump($url);
	if (!mysql_query("INSERT INTO subscriptions (title,url) VALUES ('$title', '$url')", $link)) {
		echo "Failed INSERT: " . mysql_error() . "<br/>\n";
	}
}

mysql_close($link);
?>